﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
using System.Drawing.Drawing2D;

namespace GDI_AnalysisComponents
{
    //鼠标点击类
    class GButtonDown
    {
        private PointF mousePt;

        public GButtonDown()
        {
        }

        //处理鼠标点击事件
        public string HandleGButtonDown(MouseEventArgs e, Control cl, GGraph ggraph, MoveGComponent mgc, CreateGLine createGLine)
        {
            string s = null;
            ggraph.GcSels.Clear();
            ggraph.LineSels.Clear();
            cl.Invalidate();

            switch (e.Button)
            {
                case MouseButtons.Left:
                    s=LeftDown(e, cl, ggraph, mgc, createGLine);
                    break;
                case MouseButtons.Middle:
                    break;
                case MouseButtons.Right:
                    break;
            }
            cl.Invalidate();
            return s;
        }

        //鼠标左键点击
        public string LeftDown(MouseEventArgs e, Control cl, GGraph ggraph, MoveGComponent mgc, CreateGLine createGLine)
        {
            string s = null;
            Graphics g = cl.CreateGraphics();
            mousePt = new PointF(e.X, e.Y);
            ArrayList gcSavs = ggraph.GcSavs;
            ArrayList gcSels = ggraph.GcSels;
            MouseEventMode mem = ggraph.MEM;
            
            switch (mem)
            {
                case MouseEventMode.Normal:
                    if (ClickHeadGUnit(gcSavs, mousePt) == true)
                    {
                        ggraph.GcSels.Clear();
                        GComponent gc = GetClickGC(gcSavs, mousePt);
                        HandleClickHeadGUnit(ggraph, gc);
                        ggraph.MEM = MouseEventMode.SelectComponent;
                        mgc.LButtonDown(g, mousePt, gcSels);
                    }
                    else if (ClickParaGUnit(gcSavs, mousePt) == true)
                    {
                        GUnit gun = GetClickParaGUnit(gcSavs, mousePt);
                        HandleClickParaGUnit(ggraph, gun);
                    }
                    else if (ClickGline(ggraph, mousePt) == true)
                    {
                        s = HandleClickGline(g, ggraph);
                        //gsts.MEM = MouseEventMode.SelLine;
                    }
                    else
                    {
                        ggraph.GcSels.Clear();
                    }
                    break;
                case MouseEventMode.LinkLine:
                    if (ClickParaGUnit(gcSavs, mousePt) == true)
                    {
                        GUnit gun = GetClickParaGUnit(gcSavs, mousePt);
                        HandleClickParaGUnit(ggraph, gun);
                        createGLine.LButtonDown(g, mousePt, gun, ggraph);
                        ggraph.MEM = MouseEventMode.LinkLineMove;                       
                    } 
                    break;
                case MouseEventMode.LinkLineMove:
                    if (ClickParaGUnit(gcSavs, mousePt) == true)
                    {
                        GUnit gun = GetClickParaGUnit(gcSavs, mousePt);
                        HandleClickParaGUnit(ggraph, gun);
                        s=createGLine.LButtonDown(g, mousePt, gun, ggraph);
                        ggraph.MEM = MouseEventMode.Normal;
                    }
                    else
                    {
                        GUnit gun = null;
                        createGLine.LButtonDown(g, mousePt, gun, ggraph);
                    }
                    break;
                case MouseEventMode.SelectComponent:
                    break;
                case MouseEventMode.MoveComponent:
                    break;
                case MouseEventMode.TransformShape:
                    break;
            }
            return s;
        }

        //判断是否点击组件头
        public bool ClickHeadGUnit(ArrayList gcSavs, PointF mousePt)
        {
            bool CLINKHEADGUNIT = false;
            for (int i = 0; i < gcSavs.Count; i++)
            {
                GComponent gc = (GComponent)gcSavs[i];
                RectangleF headGUnitRect = gc.GetGComponentHeadGUnitRect();
                if (headGUnitRect.Contains(mousePt))
                    CLINKHEADGUNIT = true;
            }
            return CLINKHEADGUNIT;
        }

        //获取组件头
        public bool ClickParaGUnit(ArrayList gcSavs, PointF mousePt)
        {
            bool CLINKPARAGUNIT = false;
            for (int i = 0; i < gcSavs.Count; i++)
            {
                GComponent gc = (GComponent)gcSavs[i];
                ArrayList gunList = gc.ParaGUnitList;
                for (int j = 0; j < gunList.Count; j++)
                {
                    GUnit gun = (GUnit)gunList[j];
                    RectangleF grect = gun.getGUnitRect();
                    if (grect.Contains(mousePt))
                        CLINKPARAGUNIT = true;
                }
            }
            return CLINKPARAGUNIT;
        }

        //判断是否点击连线
        public bool ClickGline(GGraph ggraph, PointF mousePt)
        {
            bool CLINKGLINE = false;
            ArrayList lineSavs = ggraph.LineSavs;
            for (int i = 0; i < lineSavs.Count; i++)
            {
                GLinkGUnits glg = (GLinkGUnits)lineSavs[i];
                GLine gl = glg.GetGLine();
                if (gl.Pick(mousePt) == true)
                    CLINKGLINE = true;
            }
            return CLINKGLINE;
        }

        //处理点击连线
        public string HandleClickGline(Graphics g, GGraph ggraph)
        {
            string s = null;
            ArrayList lineSavs = ggraph.LineSavs;
            ArrayList lineSels = ggraph.LineSels;
            for (int i = 0; i < lineSavs.Count; i++)
            {
                GLinkGUnits glg = (GLinkGUnits)lineSavs[i];
                GLine gl = glg.GetGLine();
                if (gl.Pick(mousePt) == true)
                {
                    lineSels.Add(glg);
                    s = "你选择了连线！";
                }
            }
            return s;
        }

        //获取所点击的组件
        public GComponent GetClickGC(ArrayList gcSavs, PointF mousePt)
        {
            for (int i = 0; i < gcSavs.Count; i++)
            {
                GComponent gc = (GComponent)gcSavs[i];
                RectangleF headGUnitRect = gc.GetGComponentHeadGUnitRect();
                if (headGUnitRect.Contains(mousePt))
                    return gc;
            }
            return null;
        }

        //获取所点击的单元
        public GUnit GetClickParaGUnit(ArrayList gcSavs, PointF mousePt)
        {
            for (int i = 0; i < gcSavs.Count; i++)
            {
                GComponent gc = (GComponent)gcSavs[i];
                ArrayList gunList = gc.ParaGUnitList;
                for (int j = 0; j < gunList.Count; j++)
                {
                    GUnit gun = (GUnit)gunList[j];
                    RectangleF grect = gun.getGUnitRect();
                    if (grect.Contains(mousePt))
                        return gun;
                }
            }
            return null;
        }

        //获取点击的选择矩形
        public TransformShapeMode getClickSelRect(ArrayList gcSels, PointF mousePt)
        {
            TransformShapeMode tsm = TransformShapeMode.Normal;
            for (int i = 0; i < gcSels.Count; i++)
            {
                GComponent gc = (GComponent)gcSels[i];
                for (int j = 0; j < gc.SelRects.Count; j++)
                {
                    RectangleF rect = (RectangleF)(gc.SelRects[j]);
                    if (rect.Contains(mousePt))
                    {
                        switch (j)
                        {
                            case 0:
                                tsm = TransformShapeMode.TransformShape_LU;
                                break;
                            case 1:
                                tsm = TransformShapeMode.TransformShape_RU;
                                break;
                            case 2:
                                tsm = TransformShapeMode.TransformShape_RD;
                                break;
                            case 3:
                                tsm = TransformShapeMode.TransformShape_LD;
                                break;
                        }
                    }
                }
            }
            return tsm;
        }

        //处理点击组件头
        public string HandleClickHeadGUnit(GGraph ggraph, GComponent gc)
        {
            string s = null;
            RectangleF componentRect = gc.GetGComponentRect();
            GraphicsPath gp = new GraphicsPath();
            gp.AddRectangle(componentRect);
            PointF[] p = gp.PathPoints;
            gc.SelRects.Clear();
            for (int j = 0; j < gp.PointCount; j++)
            {
                gc.SelRects.Add(new RectangleF(p[j].X - 3, p[j].Y - 3, 6, 6));
            }
            gp.Dispose();
            ggraph.GcSels.Add(gc);
            s = "你选择了组件"+gc.ID+gc.Name;
            return s;
        }

        //处理点击参数单元
        public void HandleClickParaGUnit(GGraph ggraph, GUnit gun)
        {
        }

    }
}
